package dao.impl;

import dao.ClientDao;
import entity.Client;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.DruidUtils;

import java.util.List;

public class ClientDaoImpl implements ClientDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtils.getDataSource());
    @Override
    public List<Client> selectAllClients() {
        String sql = "select * from client";
        List<Client> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Client.class));
        return query;
    }

    @Override
    public Client getClientById(Integer Clid) {
        String sql = "select * from client where Clid = ?";
        Client query = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Client.class),Clid);
        return query;
    }

    @Override
    public void addClient(Client client) {
        String sql = " insert into client values (?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                client.getClid(),1,client.getClname(),client.getCltel(),0,client.getCladdress(),client.getRemark());
    }
}
